var path = require('path');
var webpack = require('webpack');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var HtmlWebpackPlugin = require('html-webpack-plugin');
var plugins = [
    // 分离代码
    // // extract inline css into separate 'style.css'
    new ExtractTextPlugin('[name].style.css', {
        disable: false,
        allChunks: true
    }),
    // 共享代码
    new webpack.optimize.CommonsChunkPlugin( /* chunkName= */ 'vendor', /* filename= */ 'vendor.js'),
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.DefinePlugin({
        __DEV__: false,
        __DEVTOOLS__: false,
    }),
    // If you want to minify everything
    new webpack.optimize.UglifyJsPlugin({
        // 压缩代码，开启警告，用webpack -p代替
        compress: {
            warnings: false
        },
        // 混淆代码，但排除以下关键字
        mangle: {
            except: ['exports', 'require']
        }
    }),
    // 根据模板插入css/js等生成最终HTML
    new HtmlWebpackPlugin({
        // title: '数据监控平台',
        filename: 'app.html',
        template: './templates/app.html',
        inject: true,
        hash: true, // 开启追加哈希的行为
        minify: {
            removeComments: true,
            collapseWhitespace: true
        }
    }),
    // new webpack.ProvidePlugin({
    //     'fetch': 'imports?this=>global!exports?global.fetch!whatwg-fetch'
    // }),
];

module.exports = {
    devtool: 'source-map',
    entry: {
        app: ['./startup/app.jsx'],
        vendor: ['babel-polyfill', 'react']
    },
    output: {
        // path: path.join(__dirname, 'dist'),
        // filename: 'bundle.js',
        // publicPath: '/static/'
        path: path.join(__dirname, 'assets/dist/prod'),
        filename: "[name].bundle.js",
        chunkFilename: "[id].bundle.js",
        publicPath: "/static/"
    },
    resolve: {
        extensions: ['', '.js', '.jsx'],
        alias: {
            // 开发时可更换为'history/lib/createHashHistory'
            'historyFactory': 'history/lib/createBrowserHistory',
            'shim': 'es5-shim/es5-shim.min.js',
            'sham': 'es5-shim/es5-sham.min.js',
        }
    },
    plugins: plugins,
    module: {
        loaders: [{
                test: /\.js[x]?$/,
                include: __dirname,
                exclude: [
                    path.resolve(__dirname, 'node_modules')
                ],
                loader: 'babel'
        }, {
            test: /\.less|\.css$/,
            exclude: [
                path.resolve(__dirname, 'node_modules')
            ],
            loader: ExtractTextPlugin.extract("style-loader", "css-loader?modules&minimize&sourceMap!autoprefixer-loader!less-loader?sourceMap")
        }, {
            test: /\.woff(\?v=\d+\.\d+\.\d+)?$/,
            loader: 'url?limit=10000&minetype=application/font-woff'
        }, {
            test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/,
            loader: 'url?limit=10000&minetype=application/font-woff'
        }, {
            test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
            loader: 'url?limit=10000&minetype=application/octet-stream'
        }, {
            test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
            loader: 'file'
        }, {
            test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
            loader: 'url?limit=10000&minetype=image/svg+xml'
        }, {
            test: /\.(jpe?g|png|gif|svg)$/i,
            loaders: [
                'file?hash=sha512&digest=hex&name=[hash].[ext]',
                'image-webpack?bypassOnDebug&optimizationLevel=7&interlaced=false'
            ]
        }]
    }
};
